//
//  CTPersonalProfileView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/29.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTPersonalProfileView.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "CTAppLanguage.h"
#import "UIViewAdditions.h"
#import "CTSetView_Def.h"
#import "CTRequestUtil.h"

@interface CTPersonalProfileView()
{
    UIView *topView;
    UIImageView *headImageView;
    UIImageView *vipImageView;
    UILabel *vipLabel;
    UILabel *userLabel;
    UILabel *signatureLabel;
    UILabel *userIDLabel;
    UILabel *productionLabel;
    UILabel *favLabel;
    UILabel *sexLabel;
    UILabel *signautreBottomLabel;
    UIScrollView * personScrollView;
}

@end

@implementation CTPersonalProfileView
@synthesize delegate;
- (void)dealloc
{
    [personScrollView removeFromSuperview];
    personScrollView = nil;
    
    [sexLabel removeFromSuperview];
    sexLabel = nil;
    [signautreBottomLabel removeFromSuperview];
    signautreBottomLabel = nil;
    delegate = nil;
    [topView removeFromSuperview];
    topView = nil;
    [headImageView removeFromSuperview];
    headImageView = nil;
    [vipImageView removeFromSuperview];
    vipImageView = nil;
    [vipLabel removeFromSuperview];
    vipLabel = nil;
    [userLabel removeFromSuperview];
    userLabel = nil;
    [signatureLabel removeFromSuperview];
    signatureLabel = nil;
    [userIDLabel removeFromSuperview];
    userIDLabel = nil;
    [productionLabel removeFromSuperview];
    productionLabel = nil;
    [favLabel removeFromSuperview];
    favLabel = nil;
    
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTPersonalProfileView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.frame = CGRectMake(0, 0,CT_HEADER_SCREEN_WIDTH,CT_HEADER_SCREEN_HEIGHT);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        [self createSetTitleView];
        [self createPersonView];
    }
    return self;
}

//创建标题
- (void)createSetTitleView
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:topView];
    
    //标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.font = [UIFont fontWithName:nil size:CT_HEADER_TITLE_FONTSIZE];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"userProfileView_title_text" withValue:nil withTable:@"Language"];
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLabel];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
    
    UILabel * topBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topView.height-0.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topBottomLine.backgroundColor = CT_HEADER_DEEPLINE_COLOR;
    [topView addSubview:topBottomLine];
}
- (void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(personalProfileViewBackBtn:)])
    {
        [self.delegate personalProfileViewBackBtn:self];
    }
}

- (void)createPersonView
{
//header
    personScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, topView.bottom, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-topView.height)];
    personScrollView.contentSize = CGSizeMake(CT_HEADER_SCREEN_WIDTH, 400);
    [self addSubview:personScrollView];
    headImageView = [[UIImageView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-80)/2, 30, 80, 80)];
    headImageView.layer.masksToBounds = YES;
    headImageView.layer.cornerRadius  = 40;
    headImageView.image = [UIImage imageNamed:@"head"];
    [personScrollView addSubview:headImageView];
    
    vipImageView = [[UIImageView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-15)/2-22.5, headImageView.bottom+5, 15, 15)];
    vipImageView.layer.masksToBounds = YES;
    vipImageView.layer.cornerRadius  = 7.5;
    vipImageView.image = [UIImage imageNamed:@"v"];
    [personScrollView addSubview:vipImageView];
    
    vipLabel = [[UILabel alloc]initWithFrame:CGRectMake(vipImageView.right+5, headImageView.bottom+7, 40, 10)];
    vipLabel.font = [UIFont systemFontOfSize:10];
    vipLabel.textAlignment = NSTextAlignmentLeft;
    vipLabel.textColor = CTABOUTUSVIEW_VERSION_COLOR;
    vipLabel.text = [CTAppLanguage localizedStringForKey:@"userProfileView_vipLabel_text" withValue:nil withTable:@"Language"];
    [personScrollView addSubview:vipLabel];
    
    UIImageView * headRightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-25, 63, 15, 15)];
    headRightImageView.image = [UIImage imageNamed:@"about_icon_go"];
    [personScrollView addSubview:headRightImageView];
    UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [headBtn addTarget:self action:@selector(headBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    headBtn.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 170);
    [personScrollView addSubview: headBtn];
    
    UILabel *headBottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, headBtn.bottom, CT_HEADER_SCREEN_WIDTH, 0.5)];
    headBottomLabel.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [personScrollView addSubview:headBottomLabel];
    //name
    UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, headBottomLabel.bottom, CT_HEADER_SCREEN_WIDTH*0.2853, 49)];
    userNameLabel.font = [UIFont systemFontOfSize:18];
    userNameLabel.text = [CTAppLanguage localizedStringForKey:@"userProfileView_nameLabel_text" withValue:nil withTable:@"Language"];
    userNameLabel.textAlignment = NSTextAlignmentLeft;
    userNameLabel.textColor = CT_HEADER_BLACK_COLOR;
    [personScrollView addSubview:userNameLabel];
//    170
    userLabel = [[UILabel alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-170)/2, headBottomLabel.bottom, 200, 49)];
    userLabel.font = [UIFont systemFontOfSize:15];
    userLabel.text = @"温水闲闲";
    userLabel.textAlignment = NSTextAlignmentCenter;
    userLabel.textColor = CTABOUTUSVIEW_VERSION_COLOR;
    [personScrollView addSubview:userLabel];
    UIImageView * userRightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-25, headBottomLabel.bottom+17, 15, 15)];
    userRightImageView.image = [UIImage imageNamed:@"about_icon_go"];
    [personScrollView addSubview:userRightImageView];
    
    UIButton *userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [userBtn addTarget:self action:@selector(userBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    userBtn.frame = CGRectMake(0, userLabel.origin.y, CT_HEADER_SCREEN_WIDTH, 49);
    [personScrollView addSubview: userBtn];
    
    UILabel *userBottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, userBtn.bottom, CT_HEADER_SCREEN_WIDTH, 0.5)];
    userBottomLabel.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [personScrollView addSubview:userBottomLabel];
    //性别
    UILabel *sexNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, userBottomLabel.bottom, CT_HEADER_SCREEN_WIDTH*0.2853, 49)];
    sexNameLabel.font = [UIFont systemFontOfSize:18];
    sexNameLabel.text = [CTAppLanguage localizedStringForKey:@"userProfileView_sexLabel_text" withValue:nil withTable:@"Language"];
    sexNameLabel.textAlignment = NSTextAlignmentLeft;
    sexNameLabel.textColor = CT_HEADER_BLACK_COLOR;
    [personScrollView addSubview:sexNameLabel];
    //    170
    sexLabel = [[UILabel alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-170)/2, userBottomLabel.bottom, 200, 49)];
    sexLabel.font = [UIFont systemFontOfSize:15];
    sexLabel.text = @"温水闲闲";
    sexLabel.textAlignment = NSTextAlignmentCenter;
    sexLabel.textColor = CTABOUTUSVIEW_VERSION_COLOR;
    [personScrollView addSubview:sexLabel];
    UIImageView * sexRightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-25, userBottomLabel.bottom+17, 15, 15)];
    sexRightImageView.image = [UIImage imageNamed:@"about_icon_go"];
    [personScrollView addSubview:sexRightImageView];
    
    UIButton *sexBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sexBtn addTarget:self action:@selector(sexBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    sexBtn.frame = CGRectMake(0, sexLabel.origin.y, CT_HEADER_SCREEN_WIDTH, 49);
    [personScrollView addSubview: sexBtn];
    
    UILabel *sexBottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, sexBtn.bottom, CT_HEADER_SCREEN_WIDTH, 0.5)];
    sexBottomLabel.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [personScrollView addSubview:sexBottomLabel];
    //签名
    UILabel *signatureNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, sexBottomLabel.bottom, CT_HEADER_SCREEN_WIDTH*0.2853, 49)];
    signatureNameLabel.font = [UIFont systemFontOfSize:18];
    signatureNameLabel.text = [CTAppLanguage localizedStringForKey:@"userProfileView_signLabel_text" withValue:nil withTable:@"Language"];
    signatureNameLabel.textAlignment = NSTextAlignmentLeft;
    signatureNameLabel.textColor = CT_HEADER_BLACK_COLOR;
    [personScrollView addSubview:signatureNameLabel];
    //    170
    signatureLabel = [[UILabel alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-170)/2, sexBottomLabel.bottom+15, 200, 90)];
    signatureLabel.font = [UIFont systemFontOfSize:15];
    signatureLabel.lineBreakMode = NSLineBreakByWordWrapping;
    signatureLabel.numberOfLines = 3;
    signatureLabel.textAlignment = NSTextAlignmentCenter;
    signatureLabel.textColor = CTABOUTUSVIEW_VERSION_COLOR;
    [personScrollView addSubview:signatureLabel];
    UIImageView * signatureRightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-25, sexBottomLabel.bottom+17, 15, 15)];
    signatureRightImageView.image = [UIImage imageNamed:@"about_icon_go"];
    [personScrollView addSubview:signatureRightImageView];
    
    UIButton *signatureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [signatureBtn addTarget:self action:@selector(signatureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    signatureBtn.frame = CGRectMake(0, signatureLabel.origin.y, CT_HEADER_SCREEN_WIDTH, 90);
    [personScrollView addSubview: signatureBtn];
    
    signautreBottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, signatureLabel.bottom+15, CT_HEADER_SCREEN_WIDTH, 0.5)];
    signautreBottomLabel.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [personScrollView addSubview:signautreBottomLabel];
    //用户ID
//    UILabel *userIDNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, signautreBottomLabel.bottom, CT_HEADER_SCREEN_WIDTH*0.2853, 49)];
//    userIDNameLabel.font = [UIFont systemFontOfSize:18];
//    userIDNameLabel.text = [CTAppLanguage localizedStringForKey:@"userProfileView_userIDLabel_text" withValue:nil withTable:@"Language"];
//    userIDNameLabel.textAlignment = NSTextAlignmentLeft;
//    userIDNameLabel.textColor = CT_HEADER_BLACK_COLOR;
//    [personScrollView addSubview:userIDNameLabel];
//    //    170
//    userIDLabel = [[UILabel alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-170)/2, signautreBottomLabel.bottom, 200, 49)];
//    userIDLabel.font = [UIFont systemFontOfSize:15];
//    
//    userIDLabel.textAlignment = NSTextAlignmentCenter;
//    userIDLabel.textColor = CTABOUTUSVIEW_VERSION_COLOR;
//    [personScrollView addSubview:userIDLabel];
//    
//    UIButton *userIDBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [userIDBtn addTarget:self action:@selector(userIDBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    userIDBtn.frame = CGRectMake(0, userIDLabel.origin.y, CT_HEADER_SCREEN_WIDTH, 49);
//    [personScrollView addSubview: userIDBtn];
//    
//    //作品
//    UILabel *productionNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, userIDBtn.bottom, CT_HEADER_SCREEN_WIDTH*0.2853, 49)];
//    productionNameLabel.font = [UIFont systemFontOfSize:18];
//    productionNameLabel.text = [CTAppLanguage localizedStringForKey:@"userProfileView_worksLabel_text" withValue:nil withTable:@"Language"];
//    productionNameLabel.textAlignment = NSTextAlignmentLeft;
//    productionNameLabel.textColor = CT_HEADER_BLACK_COLOR;
//    [personScrollView addSubview:productionNameLabel];
//    //    170
//    productionLabel = [[UILabel alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-170)/2, userIDBtn.bottom, 200, 49)];
//    productionLabel.font = [UIFont systemFontOfSize:15];
//
//    productionLabel.textAlignment = NSTextAlignmentCenter;
//    productionLabel.textColor = CTABOUTUSVIEW_VERSION_COLOR;
//    [personScrollView addSubview:productionLabel];
////    UIImageView * productionRightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-25, signautreBottomLabel.bottom+17, 15, 15)];
////    productionRightImageView.image = [UIImage imageNamed:@"about_icon_go"];
////    [personScrollView addSubview:productionRightImageView];
//    
//    UIButton *productionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [productionBtn addTarget:self action:@selector(productionBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    productionBtn.frame = CGRectMake(0, productionLabel.origin.y, CT_HEADER_SCREEN_WIDTH, 49);
//    [personScrollView addSubview: productionBtn];
//    
//    //被赞
//    UILabel *favNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, productionBtn.bottom, CT_HEADER_SCREEN_WIDTH*0.2853, 49)];
//    favNameLabel.font = [UIFont systemFontOfSize:18];
//    favNameLabel.text = [CTAppLanguage localizedStringForKey:@"userProfileView_praiseLabel_text" withValue:nil withTable:@"Language"];
//    favNameLabel.textAlignment = NSTextAlignmentLeft;
//    favNameLabel.textColor = CT_HEADER_BLACK_COLOR;
//    [personScrollView addSubview:favNameLabel];
//    //    170
//    favLabel = [[UILabel alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-170)/2, productionBtn.bottom, 200, 49)];
//    favLabel.font = [UIFont systemFontOfSize:15];
//    
//    //    productionLabel.lineBreakMode = NSLineBreakByWordWrapping;
//    //    productionLabel.numberOfLines = 3;
//    favLabel.textAlignment = NSTextAlignmentCenter;
//    favLabel.textColor = CTABOUTUSVIEW_VERSION_COLOR;
//    [personScrollView addSubview:favLabel];
//    
//    UIButton *favBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [favBtn addTarget:self action:@selector(favBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    favBtn.frame = CGRectMake(0, favLabel.origin.y, CT_HEADER_SCREEN_WIDTH, 49);
//    [personScrollView addSubview: favBtn];
    
}
- (void)headBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(personalProfileViewHeadBtn:)])
    {
        [self.delegate personalProfileViewHeadBtn:self];
    }
}
- (void)userBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(personalProfileViewUserNameBtn:)])
    {
        [self.delegate personalProfileViewUserNameBtn:self];
    }
}
- (void)sexBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(personalProfileViewSexBtn:)])
    {
        [self.delegate personalProfileViewSexBtn:self];
    }
}
- (void)signatureBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(personalProfileViewsignatureBtn:)])
    {
        [self.delegate personalProfileViewsignatureBtn:self];
    }
}
- (void)userIDBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(personalProfileViewUserIDBtn:)])
    {
        [self.delegate personalProfileViewUserIDBtn:self];
    }
}
- (void)productionBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(personalProfileViewProducationBtn:)])
    {
        [self.delegate personalProfileViewProducationBtn:self];
    }
}
- (void)favBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(personalProfileViewFavourBtn:)])
    {
        [self.delegate personalProfileViewFavourBtn:self];
    }
}
#pragma mark -setter-
- (void)setHeadImage:(NSString *)imageStr
{

    [CTRequestUtil requestImgWithUrl:imageStr withCompletionSuccessHandler:^(UIImage *img) {
        headImageView.image = img;
    } withFailedHandler:^(NSError *error) {
        
    }];

}

- (void)setNameLabel:(NSString *)nameStr
{
    userLabel.text = nameStr;
}
- (void)setSexLabel:(NSString *)sexStr
{
    if ([sexStr isEqualToString:@"1"]) {
        sexLabel.text = @"男";
    }else if ([sexStr isEqualToString:@"2"])
    {
        sexLabel.text = @"女";
    }else{
        sexLabel.text = @"未知";
    }
    
}
- (void)setSigLabel:(NSString *)signatureStr
{
    if (signatureStr.length > 0)
    {
        signatureLabel.text = signatureStr;
        [self LabelHeightLabel:signatureLabel fontSize:15.0f];
        signautreBottomLabel.frame = CGRectMake(0, signatureLabel.bottom+15, CT_HEADER_SCREEN_WIDTH, 0.5);
    }
    else
    {
        signatureLabel.text = [CTAppLanguage localizedStringForKey:@"userProfileView_signLabel_defaultText" withValue:nil withTable:@"Language"];
        [self LabelHeightLabel:signatureLabel fontSize:15.0f];
        signautreBottomLabel.frame  = CGRectMake(0, signatureLabel.bottom+15, CT_HEADER_SCREEN_WIDTH, 0.5);
    }
}
- (void)setuserIDlabel:(NSString *)userIDStr
{
    userIDLabel.text = userIDStr;
}
- (void)setproductionLabel:(NSString *)productionStr
{
    productionLabel.text = [NSString stringWithFormat:@"%@个",productionStr];;
}
- (void)setVip:(BOOL)vipBool
{
    vipImageView.hidden = YES;
    vipLabel.hidden  = YES;
}
- (void)setFavourLabel:(NSString *)favStr
{
    favLabel.text = [NSString stringWithFormat:@"%@次",favStr];
}


- (float)LabelHeightLabel:(UILabel*)label fontSize:(float)fontSize
{
    
    CGSize  titleSize = [label.text sizeWithFont:[UIFont systemFontOfSize:fontSize]
                               constrainedToSize:CGSizeMake(CT_HEADER_SCREEN_WIDTH-20, 2000)
                                   lineBreakMode:NSLineBreakByCharWrapping];
    label.frame = CGRectMake(label.frame.origin.x,
                             label.frame.origin.y,
                             label.frame.size.width,
                             titleSize.height);
    return titleSize.height;
}
- (void)setHeaderWithImage:(UIImage *)headerImage
{
    headImageView.image = headerImage;
}
@end
